let houseId, houseIndex

Page({
  data: {
    dialogVisible: false,
    houseList: []
  },
  onShow() {
    this.getHouseList()
  },
  async getHouseList() {
    const {
      code,
      data: houseList
    } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList,
      isEmpty: houseList.length === 0
    })
  },

  swipeClose(ev) {
    // id 是为了掉接口让后端删除
    // 要想页面也保持同步有两种办法：重新调用列表的接口；前端删除
    const {
      id,
      index
    } = ev.mark
    houseId = id
    houseIndex = index
    const {
      position,
      instance
    } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  async dialogClose(e) {
    if (e.detail === 'confirm') {
      // 后端删除
      await wx.http.delete(`/room/${houseId}`)
      // 前端删除
      /* this.data.houseList.splice(houseIndex, 1)
      // 视图更新
      this.setData({
        houseList: this.data.houseList,
        isEmpty: this.data.houseList.length === 0
      }) */
      this.getHouseList()
      wx.utils.toast('删除成功')
    }
  },

  goDetail(ev) {
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${ev.mark.id}`,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})